package com.xu.ai.model.dashscope.config;

import java.util.Set;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * webmvc config
 *
 * @author xuguan
 * @since 2025/10/16
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(new AvoidRequestInterceptor()).addPathPatterns("/**");
	}

	private static class AvoidRequestInterceptor implements HandlerInterceptor {

		public static final Set<String> avoidRequestPaths = Set.of("/favicon.ico",
				"/.well-known/appspecific/com.chrome.devtools.json");

		@Override
		public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
			if (HttpMethod.GET.matches(request.getMethod()) && avoidRequestPaths.contains(request.getRequestURI())) {
				response.setStatus(HttpStatus.NO_CONTENT.value());
				return false;
			}
			return true;
		}

	}

}
